package com.saya.model;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.saya.utils.Utils;

public class Item {
	 private String id;
	  private String product;
	  private String name;
	  private String price;
	  private String type;
	  private String introduction;
	  private String usage;
	  private String note;

	  public String getProduct()
	  {
	    return this.product;
	  }
	  public void setProduct(String product) {
	    this.product = product;
	  }
	  public String getName() {
	    return this.name;
	  }
	  public void setName(String name) {
	    this.name = name;
	  }
	  public String getPrice() {
	    return this.price;
	  }
	  public void setPrice(String price) {
	    this.price = price;
	  }
	  public String getType() {
	    return this.type;
	  }
	  public void setType(String type) {
	    this.type = type;
	  }
	  public String getIntroduction() {
	    return this.introduction;
	  }
	  public void setIntroduction(String introduction) {
	    this.introduction = introduction;
	  }
	  public String getUsage() {
	    return this.usage;
	  }
	  public void setUsage(String usage) {
	    this.usage = usage;
	  }
	  public String getNote() {
	    return this.note;
	  }
	  public void setNote(String note) {
	    this.note = note;
	  }
	  public String getId() {
	    return this.id;
	  }
	  public void setId(String id) {
	    this.id = id;
	  }

	  public static String createorUpdateItem(Item item) {
	    Entity product = Utils.getEntity("Product", item.getProduct());
	    Entity e = null;
	    if (!"".equals(item.id))
	    {
	      e = Utils.getEntity("Item", item.id);
	      if (e != null)
	      {
	        e.setProperty("Name", item.getName());
	        e.setProperty("Product", item.getProduct());
	        e.setProperty("Price", item.getPrice());
	        e.setProperty("Type", item.getType());
	        e.setProperty("Introduction", item.getIntroduction());
	        e.setProperty("Usage", item.getUsage());
	        e.setProperty("Note", item.getNote());
	      }
	    }
	    else {
	      e = new Entity("Item", product.getKey());
	      e.setProperty("Name", item.getName());
	      e.setProperty("Product", item.getProduct());
	      e.setProperty("Price", item.getPrice());
	      e.setProperty("Type", item.getType());
	      e.setProperty("Introduction", item.getIntroduction());
	      e.setProperty("Usage", item.getUsage());
	      e.setProperty("Note", item.getNote());
	    }
	    if (e != null)
	    {
	      Key k = Utils.persistEntity(e);
	      return KeyFactory.keyToString(k);
	    }
	    return "";
	  }

	  public static Item getItembyKey(String id)
	  {
	    Entity entity = Utils.getEntity(id);
	    if (entity != null) {
	      Item item = new Item();
	      item.setId(id);
	      item.setName((String)entity.getProperty("Name"));
	      item.setProduct((String)entity.getProperty("Product"));
	      item.setPrice((String)entity.getProperty("Price"));
	      item.setType((String)entity.getProperty("Type"));
	      item.setIntroduction(
	        (String)entity
	        .getProperty("Introduction"));
	      item.setUsage((String)entity.getProperty("Usage"));
	      item.setNote((String)entity.getProperty("Note"));
	      return item;
	    }
	    return null;
	  }

	  public static List<Item> getItembyProduct(String product, int limit, int offset)
	  {
	    Key ancestorKey = Utils.getKey("Product", product);
	    Iterable<Entity> items = Utils.listChildren("Item", ancestorKey, offset, limit);
	    List lstItem = new ArrayList();
	    for (Entity entity : items) {
	      Item item = new Item();
	      item.setId(KeyFactory.keyToString(entity.getKey()));
	      item.setName((String)entity.getProperty("Name"));
	      item.setProduct((String)entity.getProperty("Product"));
	      item.setPrice((String)entity.getProperty("Price"));
	      item.setType((String)entity.getProperty("Type"));
	      item.setIntroduction(
	        (String)entity
	        .getProperty("Introduction"));
	      item.setUsage((String)entity.getProperty("Usage"));
	      item.setNote((String)entity.getProperty("Note"));
	      lstItem.add(item);
	    }
	    return lstItem;
	  }
}
